﻿
layui.use(['form','element','upload','table','layer'], function(){
    //直接渲染表格
    var form = layui.form;
    var table = layui.table;
    var element = layui.element;
    let upload = layui.upload;
    var xmSelect = layui.xmSelect;
    var $ = layui.$;
    //印刷
    table.render({
        elem: '#peopleTable'
        ,headers: {
            Accept: "application/json; charset=utf-8",
        }
        ,height: 700
        ,url: '/general/testzs/public/archives/salesman/getPeopleAll'
        ,cols: [[ //表头
            { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
            {field: 'cUser_Id', hide:true,title: 'id'}
            ,{field: 'cUser_Name', title: '业务员名称'}
            ,{field: 'cDept', title: '部门名称'}
            ,{field: 'cUserHand', title: '手机号码'}
            ,{field:'right', width:200, title: '操作',toolbar:"#ArchivesTool"}
        ]]
        ,response: {
            statusName: 'code' //规定数据状态的字段名称，默认：code
            , statusCode: 200 //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'count' //规定数据总数的字段名称，默认：count
            , dataName: 'data' //规定数据列表的字段名称，默认：data
        }
        ,page: true
        ,text: { none: '暂无' }
    });

    table.on('tool(peopleTable)', function(obj){
        var data = obj.data;
        if(obj.event === 'edit_power'){
            //打开详情
            edit_rbac(data);
        }else if (obj.event === 'detail'){
            //他查看的全部
            show_info(data);
        }
    });

    function edit_rbac(data){

        layer.open({
            type: 1,
            area: ['1000px', '600px'],
            offset: '65px',
            title: '编辑'+ data.cUser_Name +' 附加查看权限',
            content: $('#CurrentPerson').html(),
            success: function (layero,index) {
                $('#cUser_Name').val(data.cUser_Name);
                $('#cUser_Id').val(data.cUser_Id);
                $.ajax({
                    type: "POST",
                    url: '/general/testzs/public/archives/Salesman/getAttachPerson',
                    data: {
                        'cUser_Id':data.cUser_Id,
                        'cUser_Name':data.cUser_Name
                    },
                    traditional: true,
                    dataType:"json",
                    success: function(data){
                        if (data.code === 200){
                            // 渲染多选部门下拉框
                            var personnel = xmSelect.render({
                                el: '#personnel',
                                name: 'personnel',
                                toolbar: {
                                    show: true,
                                },
                                autoRow: true,
                                filterable: true,
                                height: '800px',
                                paging: true,
                                direction: 'down',
                                pageSize: 6,
                                layVerify: '',
                                layVerType: 'tips',
                                data: data.data
                            });
                        }
                    }
                })

                form.on('submit(CommitAttachButton)', function (dataForm) {

                    layer.confirm('确认修改查看权限吗?', function(index){
                        var loading = layer.load(0, {
                            shade: false,
                            time: 2*1000
                        });
                        $.ajax({
                            type: "POST",
                            url: '/general/testzs/public/archives/Salesman/EditAttachPerson',
                            data: {
                                "personnel": dataForm.field.personnel,
                                'cUser_Id':data.cUser_Id,
                                'cUser_Name':data.cUser_Name
                            },
                            traditional: true,
                            dataType:"json",
                            success: function(data){
                                layer.close(loading);
                                if (data.code === 200){
                                    layer.alert(data.msg)
                                }else{
                                    layer.alert(data.msg);
                                }
                            }
                        })
                        layer.close(index);
                    });
                    return false;
                })
            }
        });

    }
    function show_info(data){

        layer.open({
            type: 1,
            area: ['1000px','650px'],
            offset: '100px',
            title: data.cUser_Name +' 全部客户的详情',
            content: $('#PeopleAllArchivesHtml').html(), // 注意这里有.html()
            success: function(layero, dIndex) {
                //详情
                table.render({
                    elem: '#peopleAllArchivesTable'
                    ,headers: {
                        Accept: "application/json; charset=utf-8",
                    }
                    ,where: {
                        'cUser_Id':data.cUser_Id,
                    }
                    ,height: 550
                    ,url: '/general/testzs/public/archives/salesman/getPeopleArchivesAll'
                    ,cols: [[ //表头

                        { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
                        { title: 'printing_id', hide:true, }
                        ,{field: 'cCusCode', title: '客户编码'}
                        ,{field: 'cCusName', title: '客户名称'}
                        // ,{field: 'cCusAbbName', title: '客户简称'}
                        // ,{field: 'cCCCode', title: '客户分类名称(目前编码查询)'}
                 //       ,{field: 'cDCCode', title: '地区编码'}
                        ,{field: 'dCusDevDate', title: '发展日期'}
                        ,{field: 'cCusPerson', title: '联系人'}
                        ,{field: 'cCusPhone', title: '联系人电话'}
                    //    ,{field: 'cUserName', title: '专营业务员'}
                    //    ,{field: 'cDCCode', title: '地区编码'}
                    //    ,{field: 'cDepName', title: '分管部门名称'}
                       // ,{field:'right', title: '操作',toolbar:"#ArchivesTool"}
                    ]]
                    ,response: {
                        statusName: 'code' //规定数据状态的字段名称，默认：code
                        , statusCode: 200 //规定成功的状态码，默认：0
                        , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                        , countName: 'count' //规定数据总数的字段名称，默认：count
                        , dataName: 'data' //规定数据列表的字段名称，默认：data
                    }
                    ,page: true
                    ,text: { none: '暂无' }
                });
            }
        });

    }
    /* 表格搜索 */
    form.on('submit(SearchForm)', function (data) {
        //alert(1111);
        // 渲染项目表格

        table.render({
            elem: '#peopleTable'
            ,headers: {
                Accept: "application/json; charset=utf-8",
            }
            ,where: {
                'cUser_Name':data.field.cUser_Name,
                'cDept':data.field.cDept,
            }
            ,height: 700
            ,url: '/general/testzs/public/archives/salesman/getPeopleSearchAll'
            ,cols: [[ //表头
                { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
                {field: 'cUser_Id', hide:true,title: 'id'}
                ,{field: 'cUser_Name', title: '业务员名称'}
                ,{field: 'cDept', title: '部门名称'}
                ,{field: 'cUserHand', title: '手机号码'}
                ,{field:'right', width:200, title: '操作',toolbar:"#ArchivesTool"}
            ]]
            ,response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: 200 //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , countName: 'count' //规定数据总数的字段名称，默认：count
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            }
            ,page: true
            ,text: { none: '暂无' }
        });
        return false;
    });

});

